package com.zhilianbao.leyaogo.ui.adapter.me;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bql.recyclerview.swipe.SwipeMenuAdapter;
import com.bql.utils.DateUtils;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.model.response.me.CouponResponse;
import com.zhilianbao.leyaogo.ui.activity.me.moneyaccount.SeeAvalibleGoodsActivity;

import java.util.List;

/**
 * Created by liu on 2016/10/26.
 * 优惠券adapter
 */
public class CouponAdapter extends SwipeMenuAdapter<CouponAdapter.CouponViewHolder> {
    private List<CouponResponse> mList;
    private Context mContext;

    public CouponAdapter(Context context, List<CouponResponse> list) {
        mList = list;
        this.mContext = context;
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_coupon, parent, false);
    }

    @Override
    public CouponAdapter.CouponViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new CouponAdapter.CouponViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(CouponAdapter.CouponViewHolder holder, int position) {
        holder.setData(mContext, mList.get(position));

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    static class CouponViewHolder extends RecyclerView.ViewHolder {

        private TextView mSeeAvalibleGoods;
        private RelativeLayout mItemCoupon;
        private ImageView mIvHasUsed;
        private TextView mCouponFullLess;
        private TextView mCouponLimit;
        private TextView mEndDay;
        private final TextView mCouponMoney;
        private final TextView mCouponRen;
        private final TextView mCouponZhe;

        public CouponViewHolder(View itemView) {
            super(itemView);
            mItemCoupon = (RelativeLayout) itemView.findViewById(R.id.item_coupon);
            mSeeAvalibleGoods = (TextView) itemView.findViewById(R.id.tv_item_coupon_see_avalible_goods);
            mIvHasUsed = (ImageView) itemView.findViewById(R.id.iv_has_used);
            mCouponFullLess = (TextView) itemView.findViewById(R.id.coupon_full_less);
            mCouponLimit = (TextView) itemView.findViewById(R.id.coupon_limit);
            mEndDay = (TextView) itemView.findViewById(R.id.coupon_end_day);
            mCouponMoney = (TextView) itemView.findViewById(R.id.coupon_money);
            mCouponRen = (TextView) itemView.findViewById(R.id.coupon_ren);
            mCouponZhe = (TextView) itemView.findViewById(R.id.coupon_zhe);
        }

        public void setData(Context context, CouponResponse couponResponse) {
            int couponsType = couponResponse.getCouponsType();
            int couponsVal = (int) couponResponse.getCouponsVal();
            double couponsLimitPrice =  couponResponse.getCouponsLimitPrice();
            int dayLimit = couponResponse.getDayLimit();
            int statusCode = couponResponse.getStatusCode();
            if (couponsType == 0) {//现金券
                mCouponFullLess.setText("现金券");
                mCouponRen.setVisibility(View.VISIBLE);
                mCouponZhe.setVisibility(View.GONE);
                mCouponMoney.setText(couponsVal + "");
                mItemCoupon.setBackgroundResource(R.drawable.bg_coupon_orange);
            } else if (couponsType == 1) {//折扣券
                mCouponFullLess.setText("折扣券");
                mCouponRen.setVisibility(View.GONE);
                mCouponZhe.setVisibility(View.VISIBLE);
                mCouponMoney.setText(couponResponse.getCouponsVal()/10 + "");
                mItemCoupon.setBackgroundResource(R.drawable.bg_coupon_pink);
            }

            if (couponsLimitPrice == 0) {
                mCouponLimit.setText("优惠券使用无限制");
            } else {
                mCouponLimit.setText("订单满" + couponsLimitPrice + "元可用");
            }
            long endDay = couponResponse.getEndDay();
            long userGetDtm = couponResponse.getUserGetDtm();
            long dayNumber = couponResponse.getDayNumber();
            long dayLimitEndDay = userGetDtm + dayNumber * 24 * 3600 * 1000;
            String format = null;
            if (dayLimit == 0) {//日期段约束
                format = DateUtils.format(endDay, "yyyy-MM-dd");
            } else if (dayLimit == 1) {//日期天数约束
                format = DateUtils.format(dayLimitEndDay, "yyyy-MM-dd");
            }
            mEndDay.setText(format);

            if (statusCode == 0) {//未使用
                mIvHasUsed.setVisibility(View.GONE);
                mSeeAvalibleGoods.setVisibility(View.VISIBLE);
            } else if (statusCode == 1) {//已使用
                mItemCoupon.setBackgroundResource(R.drawable.bg_coupon_gray);
                mIvHasUsed.setImageResource(R.drawable.label_already_used);
                mIvHasUsed.setVisibility(View.VISIBLE);
                mSeeAvalibleGoods.setVisibility(View.GONE);
            } else if (statusCode == 2) {//已过期
                mItemCoupon.setBackgroundResource(R.drawable.bg_coupon_gray);
                mIvHasUsed.setVisibility(View.VISIBLE);
                mSeeAvalibleGoods.setVisibility(View.GONE);
                mIvHasUsed.setImageResource(R.drawable.label_expired);
            }
            mIvHasUsed.setAlpha(50);
            mSeeAvalibleGoods.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, SeeAvalibleGoodsActivity.class);
                    intent.putExtra("couponsId", couponResponse.getCouponsId());
                    intent.putExtra("couponsLimitPrice", couponResponse.getCouponsLimitPrice());
                    context.startActivity(intent);
                }
            });
        }

    }
}
